﻿using BlazorApp.Model;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Model.Dtos;
using WebApp.Service;
using MVC = Microsoft.AspNetCore.Mvc;

namespace WebApp.Controller
{
    [ApiController]
    [Authorize]
    public class StudentController : ControllerBase
    {
        protected IStudentService DataService { get; set; }

        public StudentController(IStudentService dataService)
            => this.DataService = dataService;

        [MVC.Route("/api/student/list")]
        [HttpPost]
        public Task<QueryResult<Student>> QueryList(QueryPageOptions<Dictionary<string, object>>? options)
        {
            return Task.FromResult(DataService.QueryList(options));
        }

        [MVC.Route("/api/student/save")]
        [HttpPost]
        public Task<ReturnResult> Save(Student student)
        {
            ReturnResult result = new ReturnResult();
            bool b = DataService.Save(student);
            if (!b)
            {
                result.Fail("操作失败");
            }
            return Task.FromResult(result);
        }

        [MVC.Route("/api/student/delete")]
        [HttpPost]
        public Task<ReturnResult> Delete(int[] ids)
        {
            ReturnResult result = new ReturnResult();
            bool b = DataService.Delete(ids);
            if (!b)
            {
                result.Fail("操作失败");
            }
            return Task.FromResult(result);
        }

    }
}
